package com.pristine.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.EmployeeDAO;
import com.pristine.domain.DepartmentMasterEntity;
import com.pristine.domain.DoctorMasterEntity;
import com.pristine.domain.EmployeeEntity;
import com.pristine.service.wsdl.DepartmentMasterVO;
import com.pristine.service.wsdl.EmployeeVO;
import com.pristine.service.wsdl.IEmployeeService;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("employeeService")
public class EmployeeServiceImpl implements IEmployeeService{

	@Autowired
	EmployeeDAO employeeDAO;
	
	@Override
	@Transactional
	public String createEmployee(EmployeeVO employeeVO) {
		EmployeeEntity employee = VOToEntityConverter.convert(employeeVO); 
		employeeDAO.add(employee);
		return employee.getId();
		
	}

	@Override
	@Transactional
	public String updateEmployee(EmployeeVO employeeVO) {
		EmployeeEntity employee = VOToEntityConverter.convert(employeeVO); 
		employeeDAO.update(employee);
		return employee.getId();
		
	}
	@Override
	@Transactional
	public EmployeeVO getEmployeeById(String id) {
		EmployeeEntity employee =  employeeDAO.find(id);
		return EntityToVOConverter.convert(employee);
	}

	@Override
	@Transactional
	public boolean deleteEmployee(String id) {
		try {
			EmployeeEntity employee =  employeeDAO.find(id);
			employeeDAO.remove(employee);
			return true;
		} catch (Exception e) {
			return false;
		}
		
	}

	@Override
	public List<EmployeeVO> getAllEmployee() {
		List<EmployeeEntity> list = employeeDAO.list();
		List<EmployeeVO> voList= new ArrayList<EmployeeVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(EmployeeEntity entity:list){
				EmployeeVO vo = EntityToVOConverter.convert(entity);
				voList.add(vo);
			}
		}
		return voList;
	}

}
